using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.AcmsControls
{
    public class ImageContentControlEdit : BaseContentControlEdit
    {
        private FileUpload fileUpload;
        private AcmsImage editImage;
        private TextBox editAltText;
        private Label editDimentions;

        private AcmsImage displayImage;
        private Label displayDimentions;
        private Label displayAltText;

        protected override void CreateDisplayControls(PlaceHolder placholder)
        {
            displayImage = new AcmsImage();
            displayImage.ID = "displayImage";
            displayImage.RenderHeight = 400;
            displayImage.RenderWidth = 500;
            placholder.Controls.Add(displayImage);

            placholder.Controls.Add(new LiteralControl("<br/>"));

            displayDimentions = new Label();
            displayDimentions.ID = "displayDimentions";
            placholder.Controls.Add(displayDimentions);

            placholder.Controls.Add(new LiteralControl("<br/>"));

            displayAltText = new Label();
            displayAltText.ID = "displayAltText";
            placholder.Controls.Add(displayAltText);
        }

        protected override void CreateEditControls(PlaceHolder placholder)
        {
            fileUpload = new FileUpload();
            fileUpload.ID = "fileUpload";
            placholder.Controls.Add(fileUpload);

            placholder.Controls.Add(new LiteralControl("<br/>"));

            editImage = new AcmsImage();
            editImage.ID = "editImage";
            editImage.RenderHeight = 400;
            editImage.RenderWidth = 500;
            placholder.Controls.Add(editImage);

            placholder.Controls.Add(new LiteralControl("<br/>"));

            editDimentions = new Label();
            editDimentions.ID = "editDimentions";
            placholder.Controls.Add(editDimentions);

            placholder.Controls.Add(new LiteralControl("<br/>"));

            Label lbl = new Label();
            lbl.AssociatedControlID = "editAltText";
            lbl.ID = "lblAltText";
            lbl.Text = "Alt Text:";
            placholder.Controls.Add(lbl);

            placholder.Controls.Add(new LiteralControl(" "));

            editAltText = new TextBox();
            editAltText.ID = "editAltText";
            placholder.Controls.Add(editAltText);
        }

        private ImageContentControlProperties properties;
        protected ImageContentControlProperties Properties
        {
            get 
            {
                if (properties == null)
                {
                    properties = new ImageContentControlProperties();
                }
                return properties;
            }
        }

        protected override BaseContentControlProperties GetProperties()
        {
            return Properties;
        }

        protected override void SetProperties(BaseContentControlProperties properties)
        {
            this.properties = properties as ImageContentControlProperties;
        }

        protected override void SaveToProperties()
        {
            if (fileUpload.HasFile)
            {
                Acms.Bll.File file = Acms.Bll.File.CreateInstance(Acms.Bll.File.FileTypes.Image, fileUpload.PostedFile, fileUpload.FileBytes);
                file.Update();
                Properties.ResourceId = file.Id;
                Properties.Width = file.Width.Value;
                Properties.Height = file.Height.Value;
            }
            Properties.AltText = editAltText.Text;
        }

        protected override void DeleteFromProperties()
        {
            if (Properties != null && Properties.ResourceId > 0)
            {
                Acms.Bll.File file = Acms.Bll.File.GetById(Properties.ResourceId);
                if (file != null)
                {
                    file.Delete();
                }
            }
        }

        protected override void DataBindChildren()
        {
            displayAltText.Text = editAltText.Text = Properties.AltText;
            editImage.FileId = displayImage.FileId = Properties.ResourceId;
            editImage.ToolTip = displayImage.ToolTip = Properties.AltText;
            if (Properties.HasResource())
            {
                string dimentions = string.Format("{0} x {1}", Properties.Width, Properties.Height);
                editDimentions.Text = displayDimentions.Text = dimentions;
                editDimentions.Visible = displayDimentions.Visible = true;
            }
            else
            {
                editDimentions.Visible = displayDimentions.Visible = false;
            }
            base.DataBindChildren();
        }
    }
}
